package com.lrz.controller;

import com.lrz.common.Result;
import com.lrz.common.StatusCode;
import com.lrz.pojo.Comment;
import com.lrz.pojo.dto.CommentDTO;
import com.lrz.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping("/all")
    public Result findAll(){
        List<Comment> list = commentService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Comment comment){
        commentService.add(comment);
        return new Result(true, StatusCode.OK, "添加评论成功");
    }

    @GetMapping("/{articleId}")
    public Result findByArticleId(@PathVariable String articleId, HttpServletRequest request){
//        request.getSession().getAttribute("");
        Long userId = Long.valueOf(2);
        List<CommentDTO> list = commentService.findByArticleId(articleId, userId);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }
}
